//
//  LFProgressHUD.m
//  YC_GRice
//
//  Created by LiuFeng on 2018/8/9.
//  Copyright © 2018年 YeCai. All rights reserved.
//

#import "LFProgressHUD.h"

#define GRMainThreadAssert() NSAssert([NSThread isMainThread], @"GRLoading needs to be accessed on the main thread.");

@interface LFProgressHUD ()

@property (nonatomic, strong) UIView *indictor;
@property (nonatomic, strong) NSTimer *delayTimer;
@property (nonatomic, assign) BOOL finished;

@end

@implementation LFProgressHUD


+ (instancetype)showHUDAddedTo:(UIView *)view animated:(BOOL)animated {
    LFProgressHUD *hud = [[LFProgressHUD alloc] initWithView:view];
    hud.removeFromSuperViewOnHide = YES;
    [view addSubview:hud];
    return hud;
}

- (instancetype)initWithView:(UIView *)view {
    NSAssert(view, @"view not be nil");
    return [self initWithFrame:view.bounds];
}

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        [self commonInit];
    }
    return self;
}

- (void)commonInit {
    _margin       = 15.f;
    _mode         = LFProgressHUDModeIndeterminate;
    _contentColor = [UIColor colorWithWhite:0.f alpha:0.7f];
    
    [self setUpViews];
    
    [self updateIndictor];
}

- (void)setUpViews {
    UIView *backgroundView = [[UIView alloc] init];
    backgroundView.backgroundColor = self.contentColor;
    backgroundView.layer.cornerRadius  = 5;
    backgroundView.layer.masksToBounds = YES;
    [self addSubview:backgroundView];
    _backgroundView = backgroundView;
    
    UILabel *textLabel      = [[UILabel alloc] init];
    textLabel.font          = [UIFont systemFontOfSize:15];
    textLabel.textColor     = self.contentColor;
    textLabel.numberOfLines = 0;
    [backgroundView addSubview:textLabel];
    _textLabel = textLabel;
}

- (void)updateIndictor {
    UIView *indictor        = self.indictor;
    BOOL isActivityIndictor = [indictor isKindOfClass:[UIActivityIndicatorView class]];
    
    switch (self.mode) {
        case LFProgressHUDModeIndeterminate:{
            if (!isActivityIndictor) {
                [indictor removeFromSuperview];
                indictor = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhiteLarge];
                [(UIActivityIndicatorView *)indictor startAnimating];
                [self.backgroundView addSubview:indictor];
            }
        }
            break;
            
        case LFProgressHUDModeCustomView:{
            [indictor removeFromSuperview];
            indictor = self.customView;
            [self.backgroundView addSubview:indictor];
        }
            break;
            
        default:{
            [indictor removeFromSuperview];
            indictor = nil;
        }
            break;
    }
    
    self.indictor = indictor;
    
    [self updateViewsColor:self.contentColor];
    [self renewalConstraints];
}

- (void)updateViewsColor:(UIColor *)color {
    self.textLabel.textColor = color;
    
    if ([self.indictor isKindOfClass:[UIActivityIndicatorView class]]) {
        [(UIActivityIndicatorView *)self.indictor setColor:color];
    }
}

-(void)setColor:(UIColor *)color {
    if (color != self.color && ![color isEqual:self.color]) {
        _color = color;
        self.backgroundView.backgroundColor = color;
    }
}

-(void)setContentColor:(UIColor *)contentColor {
    if (contentColor != self.contentColor && ![contentColor isEqual:self.contentColor]) {
        _contentColor = contentColor;
        [self updateViewsColor:contentColor];
    }
}


-(void)setMode:(LFProgressHUDMode)mode {
    if (mode != self.mode) {
        _mode = mode;
        [self updateIndictor];
    }
}

-(void)setMargin:(CGFloat)margin {
    if (margin != self.margin) {
        _margin = margin;
        [self renewalConstraints];
    }
}


-(void)setCustomView:(UIView *)customView {
    if (customView != self.customView && ![customView isEqual:self.customView]) {
        _customView = customView;
        [self updateIndictor];
    }
}

- (void)renewalConstraints {
    WEAKSELF
    [self.backgroundView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.center.equalTo(self);
    }];
    switch (self.mode) {
        case LFProgressHUDModeText:{
            [self.textLabel mas_makeConstraints:^(MASConstraintMaker *make) {
                make.edges.equalTo(weakSelf.backgroundView).offset(weakSelf.margin);
            }];
            [self.customView removeFromSuperview];
            [self.indictor removeFromSuperview];
        }
            break;
            
        default:{
            if (self.textLabel.text.length > 0) {
                [self.indictor mas_makeConstraints:^(MASConstraintMaker *make) {
                    make.centerX.equalTo(weakSelf.backgroundView.mas_centerX);
                    make.top.mas_equalTo(weakSelf.backgroundView.mas_top).offset(weakSelf.margin);
                    make.width.height.multipliedBy(1);
                    make.width.mas_lessThanOrEqualTo(40);
                }];
                
                [self.textLabel mas_makeConstraints:^(MASConstraintMaker *make) {
                    make.top.mas_equalTo(self.indictor.mas_top).offset(weakSelf.margin);
                    make.left.right.bottom.equalTo(weakSelf.backgroundView).offset(weakSelf.margin);
                }];
            }else {
                [self.backgroundView mas_makeConstraints:^(MASConstraintMaker *make) {
                    make.size.mas_equalTo(CGSizeMake(70, 70));
                }];
                
                [self.indictor mas_makeConstraints:^(MASConstraintMaker *make) {
                    make.edges.equalTo(weakSelf.backgroundView);
                }];
                
                [self.textLabel removeFromSuperview];
            }
        }
            break;
    }
}


#pragma Mark  --------------  hide GRLoading  ----------------
- (void)hideAnimated:(BOOL)animated afterDelay:(CGFloat)delay {
    [self.delayTimer invalidate];
    
    NSTimer *timer = [NSTimer timerWithTimeInterval:delay target:self selector:@selector(handleHideTimer:) userInfo:@(animated) repeats:NO];
    [[NSRunLoop currentRunLoop] addTimer:timer forMode:NSRunLoopCommonModes];
    
    self.delayTimer = timer;
}

- (void)handleHideTimer:(NSTimer *)timer {
    [self hideAnimated:[timer.userInfo boolValue]];
}

- (void)hideAnimated:(BOOL)animated {
    GRMainThreadAssert();
    self.finished = YES;
    // ... otherwise hide the HUD immediately
    self.backgroundView.alpha = 1.f;
    
    [self.delayTimer invalidate];
    
    if (self.finished) {
        self.alpha = 0.0f;
        if (self.removeFromSuperViewOnHide) {
            [self removeFromSuperview];
        }
    }
}



@end
